package com.chinasofti1.info_manager.controller;

import com.chinasofti1.info_manager.domain.Teacher;
import com.chinasofti1.info_manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
   TeacherService teacherService = new TeacherService();
    Scanner sc = new Scanner(System.in);
    //开启老师管理系统
    public  void start(){
        studentLoop:while (true){
            System.out.println("------------------欢迎来到老师管理系统----------------------");
            System.out.println("1. 添加老师2. 删除老师3. 修改老师4. 查看老师5. 退出");
            String choice = sc.next();
            switch (choice){
                case "1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    deleteTeacherById();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢使用老师管理系统");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }

   public void updateTeacher() {
        System.out.println("输入合法的id");
        String updateId =inputTeacherId();
        //手动录入更新对象
     Teacher newTch =inputTeacherInfo(updateId);
        //进行修改
       teacherService.updateTeacher(updateId,newTch);
    }

    public void deleteTeacherById() {
        String delId =inputTeacherId();
        teacherService.deleteTeacherById(delId);
        System.out.println("删除成功");
    }

   private String inputTeacherId() {
        String id ;
        while (true){
            System.out.println("请输入老师id");
            id=sc.next();
            boolean existi= teacherService.isExists(id);
            if (existi){
                System.out.println("您输入的id不存在，请重新输入");
            }else {
                break;
            }
        }
        return  id;
    }

    private void findAllTeacher() {
        Teacher[] tchs = teacherService.findAllTeacher();
        //判断是否为空
        if(tchs == null){
            System.out.println("查无信息，请添加后重试");
            return;
        }
        for (int i = 0; i <tchs.length ; i++) {
            Teacher tch =tchs[i];
            if (tch!=null){
                System.out.println("信息如下:" +  "学号:"+tch.getId()+"姓名："+tch.getName()+"年龄："+tch.getAge()+"生日："+tch.getBirthday());           }
        }

    }

    public  void addTeacher() {
        String id;
        while (true){
            System.out.println("请输入老师的id");
            id = sc.next();
            boolean flag = teacherService.isExists(id);
            if (flag) {
                System.out.println("id可以使用");
                break;
            } else {
                System.out.println("id被占用，请重新输入");
            }
        }
       //esSystem.out.println("准备添加学生");
       //封装学生对象
     Teacher tch = inputTeacherInfo(id);
       //开始添加学生到数组
       boolean result = teacherService.addTeacher(tch);
       if (result) {
           System.out.println("添加成功");
       } else {
           System.out.println("添加失败");
       }
   }
    private Teacher inputTeacherInfo(String id) {
        System.out.println("请输入老师姓名");
        String name = sc.next();
        System.out.println("请输入老师年龄");
        String age = sc.next();
        System.out.println("请输入老师生日");
        String birthday = sc.next();
        Teacher tch = new Teacher();
        tch.setId(id);
        tch.setAge(age);
        tch.setName(name);
        tch.setBirthday(birthday);
        return tch;
    }
        }



